package com.zx.baofu.alcohol.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zx.baofu.alcohol.model.*;
import com.zx.baofu.alcohol.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台管理接口
 */
@RestController
@RequestMapping(value = "/back", method = RequestMethod.GET)
public class BackController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private SettingService settingService;
    @Autowired
    private FileService fileService;
    @Autowired
    private PrizeService prizeService;
    @Autowired
    private PeriodsService periodsService;

    /**
     * 用户列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/userList")
    public String userList(Integer pageNum, Integer pageSize){
        Result result = userService.backUserList(pageNum, pageSize);
        List<User> list = JSONArray.parseArray(JSONObject.toJSONString(result.getData()), User.class);

        Map resMap = new HashMap();
        resMap.put("code", 0);
        resMap.put("msg", "");
        resMap.put("count", result.getTotalNum());
        resMap.put("data", list);

        String jsonArray = JSONArray.toJSONString(resMap);
        return jsonArray;
    }

    /**
     * 订单列表
     * @param text 检索词（订单号 手机号 全匹配）
     * @param claimed  默认-1查所有  0-未领取 1-已领取
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/orderList")
    public String orderList(String text, Integer claimed, Integer pageNum, Integer pageSize){
        Result result = orderService.orderList(text, claimed, pageNum, pageSize);
        List<UserOrder> list = JSONArray.parseArray(JSONObject.toJSONString(result.getData()), UserOrder.class);
        Map resMap = new HashMap();
        resMap.put("code", 0);
        resMap.put("msg", "");
        resMap.put("count", result.getTotalNum());
        resMap.put("data", list);

        String jsonArray = JSONArray.toJSONString(resMap);
        return jsonArray;
    }

    /**
     * 标记订单已领奖
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/claimOrder", method = RequestMethod.POST)
    public String claimOrder(Integer orderId){
        Result result = orderService.claimOrder(orderId);
        return result.getMsg();
    }

    /**
     * 获取中间概率
     * @return
     */
    @RequestMapping(value = "/getProbability")
    public String getProbability(){
        Result result = orderService.getProbability();
        int probability = Integer.parseInt(result.getData().toString());
        List<LotteryProbability> list = new ArrayList<>();
        LotteryProbability lotteryProbability = new LotteryProbability();
        lotteryProbability.setId(1);
        lotteryProbability.setProbability(probability);
        list.add(lotteryProbability);
        Map resMap = new HashMap();
        resMap.put("code", 0);
        resMap.put("msg", "");
        resMap.put("count", 1);
        resMap.put("data", list);

        String jsonArray = JSONArray.toJSONString(resMap);
        return jsonArray;
    }

    /**
     * 设置中奖概率
     * @param probability
     * @return
     */
    /*@RequestMapping(value = "/updateProbability", method = RequestMethod.POST)
    public String updateProbability(LotteryProbability probability){
        Result result = orderService.updateProbability(probability);
        return result.getMsg();
    }*/

    @RequestMapping(value = "/managerLogin", method = RequestMethod.POST)
    public Result managerLogin(String account, String password){
        Result result = userService.manageLogin(account, password);
        return result;
    }

    /**
     * 获取活动时间
     * @return
     */
    @RequestMapping(value = "/getActiveTime")
    public String getActiveTime(){
        ActiveTime at = settingService.getActiveTime();
        List<ActiveTime> list = new ArrayList<>();
        list.add(at);
        Map resMap = new HashMap();
        resMap.put("code", 0);
        resMap.put("msg", "");
        resMap.put("count", 1);
        resMap.put("data", list);

        String jsonArray = JSONArray.toJSONString(resMap);
        return jsonArray;
    }

    /**
     * 设置活动时间
     * @return
     */
    @RequestMapping(value = "/updateActiveTime" , method = RequestMethod.POST)
    public String updateActiveTime(Integer id, String prizeName, Double prizeValue, Integer prizeNum, Integer totalNum
            , String start, String end, String manufacturer, String rule, String description, String prizeStart, String prizeEnd){
        Result result = settingService.updateActiveTime(id, prizeName, prizeValue, prizeNum, totalNum, start, end, manufacturer, rule, description, prizeStart, prizeEnd);
        return result.getMsg();
    }

    @RequestMapping(value = "/getDailyLimit")
    public String getDailyLimit(){
        DailyLimit dl = settingService.getDailyLimit();
        List<DailyLimit> list = new ArrayList<>();
        list.add(dl);
        Map resMap = new HashMap();
        resMap.put("code", 0);
        resMap.put("msg", "");
        resMap.put("count", 1);
        resMap.put("data", list);

        String jsonArray = JSONArray.toJSONString(resMap);
        return jsonArray;
    }

    @RequestMapping(value = "/updateDailyLimit", method = RequestMethod.POST)
    public String updateDailyLimit(Integer id, int limit){
        Result result = settingService.updateDailyLimit(id, limit);
        return result.getMsg();
    }

    @RequestMapping(value = "/getShareMsg")
    public String getShareMsg(){
        ShareMsg sm = settingService.getShareMsg();
        List<ShareMsg> list = new ArrayList<>();
        list.add(sm);
        Map resMap = new HashMap();
        resMap.put("code", 0);
        resMap.put("msg", "");
        resMap.put("count", 1);
        resMap.put("data", list);

        String jsonArray = JSONArray.toJSONString(resMap);
        return jsonArray;
    }

    @RequestMapping(value = "/updateShareMsg", method = RequestMethod.POST)
    public String updateShareMsg(Integer id, String title, String img, String content){
        Result result = settingService.updateShareMsg(id, title, img, content);
        return result.getMsg();
    }

    @PostMapping(value = "/upload")
    public Result upload(@RequestParam("file") MultipartFile file){
        String url = fileService.upload(file);
        return new Result().success("查询成功", url);
    }

    /**
     * 查询中奖概率
     * @return josn-String
     */
    @RequestMapping(value = "getAllLottery",method = RequestMethod.GET)
    public String getAllLottery(){
        Result result=orderService.getAllLottery();
        List<LotteryProbability> list = JSONArray.parseArray(JSONObject.toJSONString(result.getData()), LotteryProbability.class);
        Map resMap = new HashMap();
        resMap.put("code", 0);
        resMap.put("msg", "");
        resMap.put("count", 1);
        resMap.put("data", list);
        String jsonArray = JSONArray.toJSONString(resMap);
        return jsonArray;
    }

    /**
     * 查询所有奖品
     * @return json-string
     */
    @RequestMapping(value = "getAllPrize",method = RequestMethod.GET)
    public String getAllPrize(){
        Result result=prizeService.getAllPrize();
        List<Prize> list = JSONArray.parseArray(JSONObject.toJSONString(result.getData()), Prize.class);
        Map resMap = new HashMap();
        resMap.put("code", 0);
        resMap.put("msg", "");
        resMap.put("count", 1);
        resMap.put("data", list);
        String jsonArray = JSONArray.toJSONString(resMap);
        return jsonArray;
    }
    /**
     * 活动期数显示
     * @return json-string
     */
    @RequestMapping(value = "getAllPeriods",method = RequestMethod.GET)
    public String getAllPeriods(){
        Result result=periodsService.getAllPeriods();
        List<Periods> list = JSONArray.parseArray(JSONObject.toJSONString(result.getData()), Periods.class);
        Map resMap = new HashMap();
        resMap.put("code", 0);
        resMap.put("msg", "");
        resMap.put("count", 1);
        resMap.put("data", list);
        String jsonArray = JSONArray.toJSONString(resMap);
        return jsonArray;
    }
}
